package com.sky.com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询当前登陆人的购物车列表
     *
     * @return
     */
    // 查询购物车列表
    @Override
    public List<ShoppingCart> getList() {
       //1. 取出登陆人
        Long userId = ThreadLocalUtil.getCurrentId();
        //2. 调用mapper查询
        return shoppingCartMapper.getByUserId(userId);
    }


    /**
     * 清空购物车
     * **/
    @Override
    public void clean() {
        //1. 取出登陆人
        Long userId = ThreadLocalUtil.getCurrentId();
        //2. 调用mapper清理
        shoppingCartMapper.clean(userId);
    }


    /**
     * 减去购物车商品数量、
     * @param shoppingCartDTO
     * **/
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
      //设置查询条件，查询当前购物项
        ShoppingCart shoppingCart = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
        shoppingCart.setUserId(ThreadLocalUtil.getCurrentId());
        ShoppingCart oldShopping = shoppingCartMapper.getOne(shoppingCart);


        //删除购物项或者数量-1
        if(oldShopping != null){
            Integer number = oldShopping.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除的昂前记录
                shoppingCartMapper.deleteById(oldShopping.getId());
            }else {
                //当前商品在购物车的中份数不为1，修改份数即可
                oldShopping.setNumber(oldShopping.getNumber()-1);
                shoppingCartMapper.updateNumber(oldShopping);
            }
        }

    }

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     **/
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        //1. dto转entity
        ShoppingCart shoppingCart = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
        //2. 将登录用户id设置到entity
        shoppingCart.setUserId(ThreadLocalUtil.getCurrentId());
        //3. 条件查询是否有此购物项
        ShoppingCart oldShoppingCart = shoppingCartMapper.getOne(shoppingCart);
        //4. 有
        if (oldShoppingCart != null) {
            //数量+1，更新数据库
            oldShoppingCart.setNumber(oldShoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumber(oldShoppingCart);
        } else {
            if (shoppingCart.getDishId() != null) {
                //5-1 菜品新增，信息补全
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());//菜名
                shoppingCart.setImage(dish.getImage());//图片
                shoppingCart.setAmount(dish.getPrice());//单价
            } else {
                //5-2 套餐新增，信息补全
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());//菜名
                shoppingCart.setImage(setmeal.getImage());//图片
                shoppingCart.setAmount(setmeal.getPrice());//  单价
            }
            //甭管是菜品还是套餐都需要数量+1和添加时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());//添加时间
            //5-3添加
            shoppingCartMapper.insert(shoppingCart);
        }
    }




}
